﻿using System;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using System.Diagnostics.Contracts;

namespace FrontEnd
{
    internal class UnityControllerFactory : DefaultControllerFactory
    {
        #region Fields

        private readonly IUnityContainer _container;		

        #endregion

        #region Constructors

        public UnityControllerFactory(IUnityContainer container)
        {
            CodeContract.Requires(container != null);
            this._container = container;
        }

        #endregion

        #region Properties

        protected IUnityContainer Container
        {
            get
            {
                return this._container;
            }
        }

        #endregion

        #region IControllerFactory Members

        protected override IController GetControllerInstance(Type controllerType)
        {
            CodeContract.Requires(controllerType != null);
            CodeContract.Requires(typeof(IController).IsAssignableFrom(controllerType));
                        
        	return (controllerType != null) ? (IController) this.Container.Resolve(controllerType) : null;
        }

        #endregion
    }
}